﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Text;
using Utils;
using Custom;

namespace Utils
{
    public static class Utilities
    {

		private static char[] seprarators = null;
		public static char[] Separators
		{
			get
			{
				if (seprarators == null)
				{
					seprarators = new char[] { ' '};
				}
				return seprarators;
			}
		}


		public static List<PostingItem> UniqueTitle(this List<PostingItem> list)
		{
			List<PostingItem> result = new List<PostingItem>();
			if (list == null || list.Count==0)
				return result;
			result.Add(list[0]);
			for (int i = 1; i < list.Count; ++i)
				if (list[i].TitleNumber != list[i - 1].TitleNumber)
					result.Add(list[i]);
			return result;
		}

		public static string FullDescription(int titleNumber)
		{
			StringBuilder sb = new StringBuilder(200);
			using (FileStream reader = new FileStream(Config.Engine.BasePath + Config.Engine.Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
			{
				StreamReader sr = new StreamReader(reader);
				sr.BaseStream.Seek(Config.Engine.GetFilePosition(titleNumber), SeekOrigin.Begin);
				while (!sr.EndOfStream)
				{
					string s = sr.ReadLine();
					if (s.IndexOf("##TITLE##") == 0)
						break;
					sb.Append(s);
					sb.Append('\n');
				}
				return sb.ToString();
			}
		}

		public static string GetTitle(int titleNumber)
		{
			using (FileStream reader = new FileStream(Config.Engine.BasePath+Config.Engine.Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
			{
				StreamReader sr = new StreamReader(reader);
				sr.BaseStream.Seek(Config.Engine.GetFilePosition(titleNumber), SeekOrigin.Begin);
				return sr.ReadLine();
			}
		}

		/*public static List<string> GetDescription(this PostingItem item,int tokenCount,int start)
		{
			List<string> result = new List<string>();
			using (FileStream reader = new FileStream(Config.Engine.BasePath+Config.Engine.Filename, FileMode.Open, FileAccess.Read, FileShare.Read))
			{
				StreamReader sr = new StreamReader(reader);
				sr.BaseStream.Seek(Config.Engine.GetFilePosition(item.TitleNumber), SeekOrigin.Begin);
				sr.ReadLine();
				int actToken = start;
				while (actToken < item.WordPosition)
				{
					string[] lineToken = sr.ReadLine().Split(Separators, StringSplitOptions.RemoveEmptyEntries);
					int i;
					for (i = 0; i < lineToken.Length && actToken < item.WordPosition; ++i)
					{
						string token = Config.Engine.GetToken(lineToken[i]);
						if (!string.IsNullOrEmpty(token))
							++actToken;
					}
					
					for (; i < lineToken.Length && tokenCount > 0; ++i)
					{
						result.Add(lineToken[i]);
						--tokenCount;
					}
				}
				while (tokenCount > 0)
				{
					if (sr.EndOfStream)
						break;
					string[] lineToken = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
					if (lineToken == null || lineToken[0] == "##TITLE##")
						break;
					for (int i = 0; i < lineToken.Length && tokenCount > 0; ++i)
					{
						result.Add(lineToken[i]);
						--tokenCount;
					}
				}	
			}
			return result;
		}*/





	}
}